package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.CategoryType;
import com.woniuxy.oldmanapi.entity.Consulter;
import com.woniuxy.oldmanapi.entity.Cost;
import com.woniuxy.oldmanapi.entity.Othercost;
import com.woniuxy.oldmanapi.service.CategoryTypeService;
import com.woniuxy.oldmanapi.service.ConsultService;
import com.woniuxy.oldmanapi.service.OtherCostService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class OtherCostController {
    @Autowired
    OtherCostService otherCostService;
    @Autowired
    CategoryTypeService categoryTypeService;


    //查询咨询者信息
    @GetMapping("/otherCost/selectOtherCost")
    public ResponseResult<PageInfo<Othercost>> selectConsulter(int now, int size, String cname){
        ResponseResult<PageInfo<Othercost>> result = new ResponseResult<>();
        try {
            PageInfo<Othercost> pageInfo = otherCostService.selectOtherCost(now, size, cname);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //更新咨询者信息
    @PostMapping("/otherCost/updateOtherCost")
    public ResponseResult<Void> updateConsulter(@RequestBody Othercost consulter){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            String s = categoryTypeService.selectByOcid(consulter.getCategory());
            consulter.setCategory(s);
            otherCostService.updateOtherCost(consulter);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //删除咨询者信息
    @GetMapping("/otherCost/deleteOtherCost/{id}")
    public ResponseResult<Void> deleteCost(@PathVariable(value = "id") int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            otherCostService.deleteOtherCost(id);
            result.setCode(200);
            result.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //新增咨询者信息
    @PostMapping("/otherCost/insertOtherCost")
    public ResponseResult<Void> insertConsulter(@RequestBody Othercost consulter){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            String category=categoryTypeService.selectByOcid(consulter.getCategory());
            consulter.setCategory(category);
            otherCostService.insertOtherCost(consulter);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }
}
